package com.myj.jim.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myj.jim.domain.Operator;
import com.myj.jim.domain.SysLog;
import com.myj.jim.domain.bootstraptable.BootstrapTable;
import com.myj.jim.domain.bootstraptable.BootstrapTableJSON;
import com.myj.jim.service.LogtypeService;
import com.myj.jim.service.SysLogService;
import com.myj.jim.util.JimUtil;

@Controller
@RequestMapping("/sysLog")
public class SysLogController {
	private static final String PAGE_AUTHORITY = "sysLogManage";
	
	private static final String NO_AUTHORITY_VIEW = "common/noAuthorityPage";
	@Autowired
	private LogtypeService logtypeService;
	
	@Autowired
	private SysLogService sysLogService;
	
	@RequestMapping("/page")
	public String page(Model model, HttpSession session, HttpServletRequest req){
		String path = req.getContextPath();
		String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/";
		Operator op = (Operator)session.getAttribute("operator");
		if(op!=null){
			if(JimUtil.haveAuthority(op, PAGE_AUTHORITY)){
				model.addAttribute("logtypeMap", JSONObject.fromObject(logtypeService.getAllLogtypeMap()));
				return "sysLog/page";
			}else{
				return NO_AUTHORITY_VIEW;
			}
		}else{
			//到登录页面
			return "redirect:"+basePath+"index.jsp";
		}
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public BootstrapTableJSON<SysLog> list(BootstrapTable bt){
		return sysLogService.findPage(bt);
	}
}
